package coffeebar;

import java.text.DateFormat;
import java.util.*;
import java.sql.*;

public class Transaction {
	LinkedList<Item> items;
	Timestamp timestamp;
	int id;

	public Transaction() {
		items = new LinkedList<Item>();
	}

	public void setTimestamp(Timestamp timestamp) {
		this.timestamp = timestamp;
	}

	public Timestamp getTimestamp() {
		return timestamp;
	}

	public Transaction(int id, Timestamp timestamp) {
		items = new LinkedList<Item>();
		this.id = id;
		this.timestamp = timestamp;
	}

	public void add(Item item) {
		Item tmp =new Item(item.getId(), item.getName(), item.getPrice(), item
				.getImage()); 
		items.add(tmp);
		Controller.getGUI().transactionControls.refreshTable();
	}

	public Iterator iterator() {
		return items.iterator();
	}

	public Object[] getTransactions() {
		Item total = new Item(0, "Total:", getTotal(), null);
		items.add(total);
		Object[] r = items.toArray();
		items.removeLast();	
		return r;
	}

	public Object[] getItems() {
		return items.toArray();
	}

	public double getTotal() {
		double total = 0;
		Iterator it = items.iterator();
		while (it.hasNext()) {
			Item item = (Item) it.next();
			total = total + item.getPrice();
		}
		return total;
	}

	public boolean isEmpty() {
		return items.isEmpty();
	}

	public Object getTime() {
		return DateFormat.getTimeInstance(DateFormat.SHORT).format(
				getTimestamp());
	}
}
